﻿using Course.Repository.Interface;
using Course.Service.EFCore.Interface;
using System.Linq.Expressions;

namespace Course.Service.EFCore.MySql
{
    /// <summary>
    /// EFCore 基础业务类 增删改查 暂时不包含事务等
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public class CouresService<T> : ICouresService<T> where T : class, new()
    {
        private readonly IRepository<T> _repository;

        public CouresService(IRepository<T> repository)
        {
            _repository = repository;
        }

        public void Add(T t)
        {
            _repository.Add(t);
        }

        public void AddRange(IEnumerable<T> ie)
        {
            _repository.AddRange(ie);
        }

        public void Delete(T t)
        {
            _repository.Delete(t);
        }

        public void DeleteRange(IEnumerable<T> ie)
        {
            _repository.DeleteRange(ie);
        }

        /// <summary>
        /// 查询所有
        /// </summary>
        /// <param name="where"></param>
        /// <returns></returns>
        public IQueryable<T> QueryAll(Expression<Func<T, bool>>? where)
        {
            return _repository.QueryAll(where);
        }

        public IQueryable<T> QueryAll<type>(Expression<Func<T, type>> order, bool isAsc = true, Expression<Func<T, bool>>? where = null)
        {
            return _repository.QueryAll(order, isAsc, where);
        }

        public IQueryable<T> QueryAll<type>(out int total, int page, int limit, Expression<Func<T, type>> order, bool isAsc = true, Expression<Func<T, bool>>? where = null)
        {
            return _repository.QueryAll(out total, (page - 1) * limit, limit, order, isAsc, where);
        }

        public int SaveChanges()
        {
            return _repository.SaveChanges();
        }

        public void Update(T t)
        {
            _repository.Update(t);
        }

        public void UpdateRange(IEnumerable<T> ie)
        {
            _repository.UpdateRange(ie);
        }
    }
}
